/**
 * 
 */
package com.gs.librarian.web.config;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import org.apache.log4j.Logger;


/**
 * @author sabuj.das
 *
 */
public class CaptchaConfiguration {
	
	private static final Logger logger = Logger.getLogger(CaptchaConfiguration.class);
	
	public static final String RECAPTCHA_CONFIG_LOCATION = "reCaptcha.config.location";
	public static final String RECAPTCHA_CONFIGURATION = "RECAPTCHA_CONFIGURATION";
	
	private String publicKey;
	private String privateKey;
	
	private boolean useProxy = false;
	private boolean useSsl = false;
	private String proxyHost;
	private String proxyPort;
	private boolean bypassValidation = false;
	
	private CaptchaConfiguration() {
		// TODO Auto-generated constructor stub
	}
	
	public static CaptchaConfiguration configure(InputStream inputStream) throws IOException{
		logger.info("Initializing reCaptcha configuration...");
		Properties properties = new Properties();
		properties.load(inputStream);
		
		CaptchaConfiguration captchaConfiguration = new CaptchaConfiguration();
		
		captchaConfiguration.setPrivateKey(properties.getProperty("reCaptcha.private.key"));
		captchaConfiguration.setPublicKey(properties.getProperty("reCaptcha.public.key"));
		
		String useProxyText = properties.getProperty("reCaptcha.use.proxy.config", "NO");
		if("YES".equals(useProxyText)){
			captchaConfiguration.useProxy = true;
		}
		captchaConfiguration.setProxyHost(properties.getProperty("reCaptcha.proxy.server"));
		captchaConfiguration.setProxyPort(properties.getProperty("reCaptcha.proxy.server.port"));
		
		String useSslText = properties.getProperty("reCaptcha.use.ssl.config", "NO");
		if("YES".equals(useSslText)){
			captchaConfiguration.useSsl = true;
		}
		
		String bypass = properties.getProperty("bypass.captcha.validation", "NO");
		if("YES".equals(bypass)){
			captchaConfiguration.bypassValidation = true;
		}
		
		return captchaConfiguration;
	}
	
	
	@Override
	protected final void finalize() throws Throwable {
		publicKey = null;
		privateKey = null;
		proxyHost = null;
		proxyPort = null;
		super.finalize();
	}

	public boolean isUseProxy() {
		return useProxy;
	}

	public void setUseProxy(boolean useProxy) {
		this.useProxy = useProxy;
	}

	public String getPublicKey() {
		return publicKey;
	}

	public void setPublicKey(String publicKey) {
		this.publicKey = publicKey;
	}

	public String getPrivateKey() {
		return privateKey;
	}

	public void setPrivateKey(String privateKey) {
		this.privateKey = privateKey;
	}

	public String getProxyHost() {
		return proxyHost;
	}

	public void setProxyHost(String proxyHost) {
		this.proxyHost = proxyHost;
	}

	public String getProxyPort() {
		return proxyPort;
	}

	public void setProxyPort(String proxyPort) {
		this.proxyPort = proxyPort;
	}

	public String toString() {
		return "Captcha";
	}

	/**
	 * @return the useSsl
	 */
	public boolean isUseSsl() {
		return useSsl;
	}

	/**
	 * @param useSsl the useSsl to set
	 */
	public void setUseSsl(boolean useSsl) {
		this.useSsl = useSsl;
	}

	/**
	 * @return the bypassValidation
	 */
	public boolean isBypassValidation() {
		return bypassValidation;
	}

	/**
	 * @param bypassValidation the bypassValidation to set
	 */
	public void setBypassValidation(boolean bypassValidation) {
		this.bypassValidation = bypassValidation;
	}
	
}
